/**
 * Created by Administrator on 2016/9/22.
 */
$(function(){
//   点击选择样本
   $(document).on('click','.samplelist .list .con .bot .left',function(){
       $(this).addClass('hover').find('img').attr('src','images/icon6.png').parents('.list').siblings().find('.bot').children('.left').removeClass('hover').children('img').attr('src','images/icon5.png')

   });
//    支付方式选择
    $('.sub-order .order-detail .mode .right .public').click(function(){
        $(this).addClass('hover').siblings().removeClass('hover');
        $(this).find('.radio').attr('src','../hospital_nurse/images/icon41.png').parent().siblings().find('.radio').attr('src','../hospital_nurse/images/icon40.png');
    });
//    新建地址确定
    $('.newAddress .determine').click(function(){
        var address_id = geneticTesting.getQueryString('id');
        var name = $('.newAddress .con .name input').val();
        var phone = $('.newAddress .con .phone input').val();
        var add_infor = $('.newAddress .con .add-infor input').val();
        if(name==''){
            geneticTesting.layer('请输入收货人名字');
        }else if(phone==''){
            geneticTesting.layer('请输入收货人联系电话');
        }else if(!obj.phone.test(phone)){
            geneticTesting.layer('联系电话格式不正确');
        }else if(add_infor ==''){
            geneticTesting.layer('请输入详细收货地址');
        }else{
            if(address_id){
                var options = {
                    url: "http://ekang.ren/address/add",
                    target : '#output',
                    dataType:'json',
                    data:{
                        uid:1,
                        address_id:address_id
                    },
                    success: function (res) {
                        if(res.ret==0){
                            location.href='editAdd.html'
                            console.log(res)
                        }else{
                            console.log(res.msg)
                        }
                    }
                };
            }else{
                var options = {
                    url: "http://ekang.ren/address/add?uid=1",
                    target : '#output',
                    dataType:'json',
                    success: function (res) {
                        if(res.ret==0){
                            console.log(res)
                        }else{
                            console.log(res.msg)
                        }
                    }
                };
            }
            $("#form1").ajaxSubmit(options);
        }
    });
//    编辑地址滑动删除
    $(document).on('swipeleft','.editadd .addlist .con',function(){
        $(this).parent().animate({'left':'-20vw'});
    });
    $(document).on('swiperight','.editadd .addlist .con',function(){
        $(this).parent().animate({'left':'0'});
    });
//    radio切换
    $(document).on('touchstart','.editadd .addlist .radio',function(){
        $(this).attr('src','images/icon6.png').parents('.addlist').siblings().find('.radio').attr('src','images/icon5.png');
    });
//    删除地址列表
    $(document).on('click','.editadd .addlist .del',function(){
        $(this).parent().animate({'left':'-120vw'},500,function(){
            $(this).remove();
        });
        var address_id = $(this).data('id');
        geneticTesting.get('address/del',{uid:1,address_id:address_id},function(res){
            if(res.ret==0){
                console.log(res);
            }
            else{
                console.log(res)
                console.log(res.msg);
            }
        });
    });
});
//正则验证
obj={
    "phone":/^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/,
    "email":/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/,
    "name":/^[\u4e00-\u9fa5]+$/,
    "num":/^[1-9]+[0-9]*]*$/,
    "Idnumber":/^\d{17}[\w\d]$/,
};
//公用对象
url = 'http://ekang.ren/'
geneticTesting = {
//    链接
    link : function(url){
        location.href = url;
    },
//    获取连接参数
    getQueryString : function(name){
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(decodeURI(r[2])); return null;
    },
//    弹层
    layer : function (content){
        $('body').append('<div class="elasticlayer animatescale" style=" z-index: 9999999999999; position: fixed; top:0; width: 100%; height:100%; background:rgba(0,0,0,.7) "><div class="elasticlayercon" style=" background: rgba(255,255,255,1); border-radius: 5px; width: 70%;position: absolute; left: 50%; top: 50%; -webkit-transform:translate(-50%, -50%);"><div class="tit" style=" width: 100%; color:#35424b; text-align: center; padding: 5% 0; font-size: 5vw">易康医疗温馨提醒</div><p style=" width: 90%; margin: auto; text-align: center; font-size: 4.5vw; padding: 5% 0 10% 0; ">'+content+'</p><button style=" display: block; background: none; border: none; padding: 5% 0; font-size:5.5vw; color: #00b57d; width: 100%; border-top: 1px solid #ddd; letter-spacing: 1px;">ok</button></div></div>')
        $(document).on('click','.elasticlayer .elasticlayercon button',function(){
            $('.elasticlayer').remove();
            $('.publicmask').remove();
        });
    },
//    get
    get : function (texturl,data,success){
        $.ajax({
            type : 'get',
            url : url+texturl,
            dataType : 'jsonp',
            data : data,
            success : function(response) {
                success(response);
            }
        });
    },
//    首页接口
    index : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                new Vue({
                    el: '#gene',
                    data: {
                        tit: data.top.title,
                        desc : data.top.desc,
                        box : data.list
                    }
                });
            }
            else{
                console.log(res.msg);
            }
        });
    },
//    编辑地址
    newAddress : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                $('.newAddress .con .name input').val(data.user_name);
                $('.newAddress .con .phone input').val(data.mobile);
                $('.newAddress .con .add-infor input').val(data.address_desc);
            }
            else{
                console.log(res)
                console.log(res.msg);
            }
        });
    },
//    地址列表
    addlist : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                new Vue({
                    el: '#editadd',
                    data: {
                        list:data
                    },
                    methods: {
                        link: function (id) {
                            location.href = 'newAddress.html?id='+id
                        },
                        link2: function (id) {
                            location.href = 'submitorder.html?id='+id
                        }
                    }
                });
            }
            else{
                console.log(res.msg);
            }
        });
    },
//    订单预览
    ord_preview : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                var gene_id = geneticTesting.getQueryString('gene_id');
                $('body').attr('data-sp',data.promotion_price)
                new Vue({
                    el: '#sub-order',
                    data: {
                        address_desc:data.address_desc,
                        user_name : data.user_name,
                        mobile : data.mobile,
                        total_amount: data.total_amount,
                        gene_name:data.gene_name,
                        gene_img_pic:data.gene_img_pic,
                        is_have_address : data.is_have_address,
                        address_id : data.address_id,
                        promotion_price : data.promotion_price
                    },
                    methods: {
                        link: function (id) {
                            location.href = '../geneticTesting/editAdd.html?id='+id+'&gene_id='+gene_id
                        },
                        link2: function () {
                            location.href = '../geneticTesting/newAddress.html?gene_id='+gene_id
                        }
                    }
                });
            }
            else{
                console.log(res.msg);
            }
        });
    },
    //    订单预览
    ord_preview2 : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                var gene_id = geneticTesting.getQueryString('gene_id');
                $('.sub-order .botpay .left .text .span_1').html('￥'+data.total_amount);
                $('body').attr('data-sp',data.promotion_price);
                new Vue({
                    el: '#sub-order-shop',
                    data: {
                        total_amount: data.total_amount,
                        address_desc:data.address_list.address_desc,
                        user_name : data.address_list.user_name,
                        mobile : data.address_list.mobile,
                        is_have_address : data.is_have_address,
                        address_id : data.address_list.address_id,
                        //promotion_price : data.promotion_price,
                        product_list : data.product_list
                    },
                    methods: {
                        link: function (id) {
                            location.href = '../geneticTesting/editAdd.html?id='+id+'&type=shop'
                        },
                        link2: function () {
                            location.href = '../geneticTesting/newAddress.html?type=shop'
                        }
                    }
                });
            }
            else{
                console.log(res.msg);
            }
        });
    },
    get_door_doctor : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                var door_id = publicJs.getQueryString('id');

                $('.sub-order .botpay .right').attr('data-address_id',data.address_id);
                $('.sub-order .botpay .left .text .span_1').html('￥'+data.total_amount+'/'+data.minute+'分钟');
                new Vue({
                    el: '#sub-order-shop',
                    data: {
                        is_have_address : data.is_have_address,
                        user_name:data.user_name,
                        address_desc:data.address_desc,
                        mobile:data.mobile,
                        total_amount:data.total_amount,
                        category_name:data.category_name
                    },
                    methods: {
                        link: function (id) {
                            var time = $('#mydate').val();
                            $.cookie('time',time,{expires:7,path:'/'});
                            location.href = '../geneticTesting/editAdd.html?type=get_door_doctor&door_id='+door_id;
                        },
                        link2: function () {
                            location.href = '../geneticTesting/newAddress.html?type=get_door_doctor&door_id='+door_id;
                        }
                    }
                });
            }else{
                console.log(res.msg);
            }
        });
    }
}